﻿package net.wangit.view.tree;

import java.util.ArrayList;
import java.util.List;

import net.wangit.support.TreeWriter;
import net.wangit.util.Checker;
import net.wangit.util.ClassUtil;
import net.wangit.view.tree.config.TreeActionType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class Tree {

	private static Log log = LogFactory.getLog(Tree.class);
	private TreeConfig config;
	private boolean enabled;
	private String id;
	private String name;
	private String action;
	private int actionType;
	private String target;
	private String title;
	private String css;
	private String checkValue;
	private String checkAttribute;
	private String attribute;
	private String openIcon;
	private String icon;
	private String accessKey;
	private String targetDomain;
	private String targetUnitId;
	private String targetUnitShowMode;
	private int filterMode;
	private List<Tree> leafs;

	public Tree() {
		enabled = true;
		actionType = TreeActionType.LINK;
		attribute = "";
	}

	public String write() {
		String writerClassName = config.getWriter();
		TreeWriter writer;
		if (Checker.isEmpty(writerClassName)) {
			writer = new DefaultTreeWriter();
		} else {
			try {
				writer = (TreeWriter) ClassUtil.loadClass(writerClassName)
						.newInstance();
			} catch (Exception e) {
				log.error(new StringBuilder("could not load TreeWriter: ")
						.append(writerClassName).append(" for ")
						.append(config.getDomain()).append("/")
						.append(config.getTreeId()).toString());
				throw new TreeConfigurationException((new StringBuilder(
						"could not load TreeWriter: ")).append(writerClassName)
						.toString(), config.getDomain(), config.getTreeId(),
						"writer", e);
			}
		}
		return writer.write(this);
	}

	public int size() {
		int count = 1;
		if (!Checker.isEmpty(leafs)) {
			for (int i = 0; i < leafs.size(); i++) {
				Tree leaf = (Tree) leafs.get(i);
				count += leaf.size();
			}

		}
		return count;
	}

	public int countLeaf() {
		if (Checker.isEmpty(leafs))
			return 0;
		else
			return getLeafCount();
	}

	private int getLeafCount() {
		int count = 1;
		if (!Checker.isEmpty(leafs)) {
			for (int i = 0; i < leafs.size(); i++) {
				Tree leaf = (Tree) leafs.get(i);
				count += leaf.getLeafCount();
			}

			count--;
		}
		return count;
	}

	public void add(Tree tree) {
		if (leafs == null)
			leafs = new ArrayList<Tree>();
		leafs.add(tree);
	}

	public List<Tree> getLeafs() {
		return leafs;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getOpenIcon() {
		return openIcon;
	}

	public void setOpenIcon(String openIcon) {
		this.openIcon = openIcon;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public TreeConfig getConfig() {
		return config;
	}

	protected void setConfig(TreeConfig config) {
		this.config = config;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}

	public int getActionType() {
		return actionType;
	}

	public void setActionType(int actionType) {
		this.actionType = actionType;
	}

	public String getCheckValue() {
		return checkValue;
	}

	public void setCheckValue(String checkValue) {
		this.checkValue = checkValue;
	}

	public String getCheckAttribute() {
		return checkAttribute;
	}

	public void setCheckAttribute(String checkAttribute) {
		this.checkAttribute = checkAttribute;
	}

	public String getTargetDomain() {
		return targetDomain;
	}

	public void setTargetDomain(String targetDomain) {
		this.targetDomain = targetDomain;
	}

	public int getFilterMode() {
		return filterMode;
	}

	public void setFilterMode(int filterMode) {
		this.filterMode = filterMode;
	}

	public String getTargetUnitId() {
		return targetUnitId;
	}

	public void setTargetUnitId(String targetUnitId) {
		this.targetUnitId = targetUnitId;
	}

	public String getTargetUnitShowMode() {
		return targetUnitShowMode;
	}

	public void setTargetUnitShowMode(String targetUnitShowMode) {
		this.targetUnitShowMode = targetUnitShowMode;
	}

}
